package com.fang.networkdisk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fang.common.common.CommonPage;
import com.fang.networkdisk.entity.bo.LoginRecordBo;
import com.fang.networkdisk.entity.po.TbNetDisLoginRecord;
import com.fang.networkdisk.entity.vo.LoginRecordVo;
import com.fang.networkdisk.mapper.TbNetDisLoginRecordMapper;
import com.fang.networkdisk.service.TbNetDisLoginRecordService;
import com.fang.common.util.IntegerUtils;
import com.fang.networkdisk.util.NDBeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 登录记录表 服务实现类
 * </p>
 *
 * @author Bernie_fang
 * @since 2024-12-07
 */
@Service
public class TbNetDisLoginRecordServiceImpl extends ServiceImpl<TbNetDisLoginRecordMapper, TbNetDisLoginRecord> implements TbNetDisLoginRecordService {

    @Resource
    private TbNetDisLoginRecordMapper tbNetDisLoginRecordMapper;

    @Override
    public CommonPage queryLoginRecord(LoginRecordBo loginRecordBo) {
        QueryWrapper<TbNetDisLoginRecord> wrapper = new QueryWrapper<>();
        wrapper.eq(IntegerUtils.isNotEmpty(loginRecordBo.getId()),"id", loginRecordBo.getId())
                .eq(StringUtils.isNotEmpty(loginRecordBo.getUserId()),"user_id", loginRecordBo.getUserId())
                .eq(StringUtils.isNotEmpty(loginRecordBo.getUsername()),"username", loginRecordBo.getUsername())
                .eq(StringUtils.isNotEmpty(loginRecordBo.getResult()),"result", loginRecordBo.getResult())
                .eq(StringUtils.isNotEmpty(loginRecordBo.getReason()),"reason", loginRecordBo.getReason())
                .orderByDesc("created_time");
        Page<TbNetDisLoginRecord> page = new Page<>(loginRecordBo.getCurPage(), loginRecordBo.getPageSize());
        this.tbNetDisLoginRecordMapper.selectPage(page, wrapper);
        List<LoginRecordVo> resultList = NDBeanUtils.copyListProperties(page.getRecords(), LoginRecordVo.class);
        CommonPage result = new CommonPage(loginRecordBo.getCurPage(), page.getTotal(), resultList);
        return result;
    }
}
